/**
 * @author Francesco di Dio
 * Date: 20/apr/2013
 * Titolo: AccountJTreePanel.java
 * Versione: 
 */

/**
 * Copyright (c) 2013 Francesco di Dio.
 * tabuto83@gmail.com 
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.tabuto.jkeys.views;

import java.awt.BorderLayout;
import java.util.Observable;

import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import org.apache.log4j.Logger;

import com.tabuto.jkeys.JKeysConstant;
import com.tabuto.jkeys.controllers.JKControllerInterface;
import com.tabuto.jkeys.models.AbstractJKeyModel;
import com.tabuto.jkeys.models.AccountGroup;

/**
 * @author user
 * 
 */
public class AccountsTreePanel extends JPanel {
	
	private static final Logger log = Logger.getLogger(AccountsTreePanel.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 8169772468304286516L;

	private DefaultMutableTreeNode root = null;

	private JTree tree = null;

	private JKControllerInterface jKeysController = null;

	public AccountsTreePanel(JKControllerInterface jkeysController) {
		super();
		setLayout(new BorderLayout(5, 5));
		// Imposto le dimensioni
		setSize(300, 300);
		this.setJKeysController(jkeysController);

		this.setVisible(true);

	}

	private void initComponent() {
		root = new DefaultMutableTreeNode(JKeysConstant.APPLICATION_NAME);
		root = generateNodes(root);
		tree = new JTree(root);
		tree.addMouseListener(this.getJkeysController()
				.getTreeAccountsMouseAdapter(tree));
		this.add(tree);

	}

	public void refresh() {
		log.debug("Refresh");
		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
		root.removeAllChildren();
		root = generateNodes(root);
		model.reload();

		// espando tutti i nodi
		for (int i = 0; i < tree.getRowCount(); i++) {
			tree.expandRow(i);
		}

		this.repaint();
	}

	private DefaultMutableTreeNode generateNodes(DefaultMutableTreeNode top) {
		DefaultMutableTreeNode parent = null;
		DefaultMutableTreeNode child = null;

		AccountGroup rootGroup = jKeysController.getRoot();

		parent = new DefaultMutableTreeNode(rootGroup);
		top.add(parent);

		for (AbstractJKeyModel next : rootGroup.asList()) {
			child = new DefaultMutableTreeNode(next);
			parent.add(child);
		}

		return top;
	}

	public JKControllerInterface getJkeysController() {
		return jKeysController;
	}

	public void setJKeysController(JKControllerInterface jkeysController) {
		this.jKeysController = jkeysController;
		initComponent();
	}

	
	public void update(Observable o, Object arg) {
		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
		root.removeAllChildren();
		root = generateNodes(root);
		model.reload();

		// espando tutti i nodi
		for (int i = 0; i < tree.getRowCount(); i++) {
			tree.expandRow(i);
		}

		this.repaint();

	}

}
